from itsdangerous import BadSignature, SignatureExpired, TimedJSONWebSignatureSerializer

from apps.a_common.log import MyLogger
from config import SECRET_KEY

logger = MyLogger('jwt auth')
TIMEOUT = 60 * 60

s = TimedJSONWebSignatureSerializer(SECRET_KEY, expires_in=TIMEOUT)


def decode_token(token) -> (dict, bool):
	try:
		return s.loads(token), True
	except BadSignature:
		logger.info('decode jwt fail, detail: {}'.format(token))
		return {}, False
	except SignatureExpired:
		logger.info('token expire')
		return {}, False
	except Exception as e:
		logger.error(f"unknown jwt decode error: {str(e)}")
		return {}, False


def encode_token(user_id):
	return s.dumps(user_id)
